﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using System.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eConfigurationSettings : EditBase
    {
        #region Public Members

        public RadComboBox ddCategory { get; set; }
        public RadComboBox ddSubCategory { get; set; }



        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }


        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {

            ConfigurationController cc = new ConfigurationController();
            ConfigurationInfo ci = cc.SelectConfiguration(ActiveItemId);

            if (ci != null)
            {
                lblHeaderLeft.Text = "Configuration - " + ci.Name;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);

            BindGridItems();
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
        }

        #endregion

        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("SettingId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eConfigurationSetting", id, ActiveItemId));
            }
        }

        #endregion

        public void BindGridItems()
        {
            SettingController sc = new SettingController();
            List<SettingInfo> siList;

            ConfigurationSettingController csc = new ConfigurationSettingController();
            List<ConfigurationSettingInfo> csList = new List<ConfigurationSettingInfo>();

            siList = sc.SelectSettings("Name Asc");

            foreach (SettingInfo si in siList)
            {
                si.FillDetails();
                ConfigurationSettingInfo csi = csc.SelectConfigurationSetting(ActiveItemId, si.SettingId);
                if (csi == null)
                {
                    csi = new ConfigurationSettingInfo(ActiveItemId, si.SettingId, false, "", "", "", "");
                }

                csi.Category = si.Category;
                csi.Subcategory = si.Subcategory;
                csi.Setting = si.Name;
                csList.Add(csi);
            }

            rgListItems.DataSource = csList;
            rgListItems.DataBind();

            sc = null;
            csc = null;


        }


        #region Action Events

       

        private void DeleteItem(Guid id)
        {
            ConfigurationSettingController csc = new ConfigurationSettingController();
            csc.DeleteConfigurationSetting(ActiveItemId, id);
            csc = null;

            RaiseNavEvent(this, new NavEventArgs("eConfigurationSettings", ActiveItemId));
        }


        #endregion


        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eConfigurationSetting", new Guid(rgListItems.SelectedValue.ToString()), ActiveItemId));
                    }

                    break;
                case "Clear":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                case "Return":
                    RaiseNavEvent(this, new NavEventArgs("eConfigurations", ActiveItemId));
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Edit", "Edit Setting", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Clear", "Clear Setting", "~/images/icons/delete.png", "Are you sure you wish to clear this setting?", true));
                al.Add(new ActionInfo("Return", "Return", "~/images/icons/undo.png"));


                return al;
            }
        }

        #endregion
    }
}